package com.telecomyt.jwportal.activity.search;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.telecomyt.jwportal.R;
import com.telecomyt.jwportal.activity.search.bean.SearchHistoryBean;
import com.telecomyt.jwportal.base.BaseActivity;
import com.telecomyt.jwportal.base.BasePresenter;
import com.telecomyt.jwportal.widget.flowlayout.CustomFlowLayout;
import com.telecomyt.jwportal.widget.flowlayout.TagItemView;

import org.litepal.LitePal;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity {


    @BindView(R.id.mImgSearchBack)
    ImageView mImgSearchBack;
    @BindView(R.id.edtext_search)
    EditText edtextSearch;
    @BindView(R.id.activity_search)
    TextView activitySearch;
    @BindView(R.id.mRelativeLayout)
    RelativeLayout mRelativeLayout;
    @BindView(R.id.RelativeLayout_s)
    RelativeLayout RelativeLayoutS;
    @BindView(R.id.mDeleteAllData)
    ImageView mDeleteAllData;
    @BindView(R.id.mCustomFlowLayout)
    CustomFlowLayout mCustomFlowLayout;
    @BindView(R.id.activity_search_layout_default)
    LinearLayout activitySearchLayoutDefault;

    public static void start(Context context) {
        Intent starter = new Intent(context, SearchActivity.class);
        context.startActivity(starter);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected BasePresenter creatPresenter() {
        return null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        getSearchHistory();
    }

    @OnClick({R.id.activity_search, R.id.mDeleteAllData})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_search:
                intentResultActivity(edtextSearch.getText().toString().trim());
                break;
            case R.id.mDeleteAllData:
                deleteAllHistoryData();
                break;
        }
    }

    private void intentResultActivity(String key) {
        saveHistorydata(key);
        SearchResultActivity.start(this);

    }

    private void saveHistorydata(String key) {
        List<SearchHistoryBean> mLocalHistory = LitePal.findAll(SearchHistoryBean.class);
        if (mLocalHistory != null && mLocalHistory.size() > 0) {
            for (SearchHistoryBean historyBean : mLocalHistory) {
                if (key.equals(historyBean.getKey())) {
                    historyBean.delete();
                }
            }
            if (mLocalHistory.size() > 20) {
                LitePal.findFirst(SearchHistoryBean.class).delete();
            }
        }
        SearchHistoryBean historyBean = new SearchHistoryBean();
        historyBean.setKey(key);
        historyBean.save();
    }

    public void getSearchHistory() {
        List<SearchHistoryBean> all = LitePal.findAll(SearchHistoryBean.class);
        showHistoryData(all);
    }

    public void showHistoryData(List<SearchHistoryBean> list) {
        mCustomFlowLayout.removeAllViews();
        Collections.reverse(list);
        for (int i = 0; i < list.size(); i++) {
            mCustomFlowLayout.addView(getLabel(list.get(i).getKey()),
                    new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT));
        }

    }

    private TagItemView getLabel(final String text) {
        TagItemView view = new TagItemView(this);
        view.setTitle(text);
        view.setOnClickListener(v -> {
            intentResultActivity(text);
        });
        return view;
    }

    public void deleteAllHistoryData() {
        LitePal.deleteAll(SearchHistoryBean.class);
        mCustomFlowLayout.removeAllViews();

    }
}
